package com.tqz.rc.server.controller;

import com.tqz.rc.api.Instance;
import com.tqz.rc.common.exception.RcException;
import com.tqz.rc.common.result.Result;
import com.tqz.rc.server.core.InstanceOperator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *
 * @author tianqingzhao
 * @since 2021/8/26 13:51
 */
@RestController
@RequestMapping("instance")
public class InstanceController {

    @Autowired
    private InstanceOperator instanceOperator;

    @PostMapping("register")
    public Result retister(String namespaceId, Instance instance) throws RcException { // TODO 该方法接收参数不要用对象
        instanceOperator.registerInstance(namespaceId, instance);
        return new Result(200, "注册成功！");
    }

    @GetMapping("listAllInstance1")
    public Object listAllInstance(String namespaceId) {
        return new Result(instanceOperator.selectInsatnces(namespaceId));
    }

    @GetMapping("selectInstances")
    public Result selectInsatnces(String namespaceId, String serviceName, String groupName) {
        return new Result(instanceOperator.selectInsatnces(namespaceId, serviceName, groupName));
    }

    @PutMapping("beat")
    public Result beat(String namespaceId, String serviceName, String clusterName, String ip, int port) {
        instanceOperator.beat(namespaceId, serviceName, clusterName, ip, port);
        return new Result();
    }
}
